package com.auctionhaus

import grails.test.*
import java.util.Formatter.DateTime

class ListingTests extends GrailsUnitTestCase {
    transient Date testFutureDate =  new Date()+1;
    transient Date testCurrentDate =  new Date();

    protected void setUp() {
        super.setUp()
    }

    protected void tearDown() {
        super.tearDown()
    }

    void testListingNameMissing() {

        mockDomain(Listing)
        def l = new Listing(creationDate:testCurrentDate, endDateTime:testFutureDate, startBidPrice:0, seller:new Customer())
        l.validate()
        assertTrue l.hasErrors()
        assertTrue l.errors.hasFieldErrors("listName")
    }
    void testListingNameValid() {
        mockDomain(Listing)
        def l = new Listing(creationDate:testCurrentDate, listName:"2001 Jeep Grand Cherokee",endDateTime:testFutureDate, startBidPrice:0, seller:new Customer())
        l.validate()
        assertFalse l.hasErrors()
        assertFalse l.errors.hasFieldErrors("listName")
    }

    void testEndDateTimeMissing() {

        mockDomain(Listing)
        def l = new Listing(creationDate:testCurrentDate, listName:"2001 Jeep Grand Cherokee", startBidPrice:0, seller:new Customer())
        l.validate()
        assertTrue l.hasErrors()
        assertTrue l.errors.hasFieldErrors("endDateTime")
    }
    void testEndDateTimeValid() {
        mockDomain(Listing)
        def l = new Listing(creationDate:testCurrentDate, listName:"2001 Jeep Grand Cherokee",endDateTime:testFutureDate, startBidPrice:0, seller:new Customer())
        l.validate()
        assertFalse l.hasErrors()
        assertFalse l.errors.hasFieldErrors("endDateTime")
    }

     void testStartBidPriceMissing() {
        mockDomain(Listing)
        def l = new Listing(creationDate:testCurrentDate, listName:"2001 Jeep Grand Cherokee",endDateTime:testFutureDate, seller:new Customer())
        l.validate()
        assertTrue l.hasErrors()
        assertTrue l.errors.hasFieldErrors("startBidPrice")
        assertNull l.startBidPrice
    }
    void testStartBidPriceValid() {
        mockDomain(Listing)
        def l = new Listing(creationDate:testCurrentDate, listName:"2001 Jeep Grand Cherokee",endDateTime:testFutureDate, startBidPrice:1, seller:new Customer())
        l.validate()
        assertFalse l.hasErrors()
        assertFalse l.errors.hasFieldErrors("startBidPrice")
        assertNotNull l.startBidPrice
    }
    void testOptionalDescriptionMissing() {
        mockDomain(Listing)
        def l = new Listing(creationDate:testCurrentDate, listName:"2001 Jeep Grand Cherokee",endDateTime:testFutureDate, startBidPrice:1, seller:new Customer())
        l.validate()
        assertFalse l.hasErrors()
        assertFalse l.errors.hasFieldErrors("description")
        assertNull l.description
    }
    void testOptionalDescriptionSet() {
        mockDomain(Listing)
        def l = new Listing(creationDate:testCurrentDate, listName:"2001 Jeep Grand Cherokee",endDateTime:testFutureDate, startBidPrice:1, seller:new Customer())
        l.description = "Shiny, Burgandy, Rust-Free"
        l.validate()
        assertFalse l.hasErrors()
        assertFalse l.errors.hasFieldErrors("description")
        assertNotNull l.description
    }
    void testSellerMissing() {
        mockDomain(Listing)
        def l = new Listing(creationDate:testCurrentDate, listName:"2001 Jeep Grand Cherokee",endDateTime:testFutureDate, startBidPrice:1)
        l.validate()
        assertTrue l.hasErrors()
        assertTrue l.errors.hasFieldErrors("seller")
        assertNull l.seller
    }
    void testSellerValid() {
        mockDomain(Listing)
        def l = new Listing(creationDate:testCurrentDate, listName:"2001 Jeep Grand Cherokee",endDateTime:testFutureDate, startBidPrice:1, seller:new Customer())
        l.validate()
        assertFalse l.hasErrors()
        assertFalse l.errors.hasFieldErrors("seller")
        assertNotNull l.seller
    }
}
